﻿//----------------------------------------------------

#region 命名空间

//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WinnerCMS.Common;
using WinnerCMS.Data;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL
{
    /// <summary>
    /// 业务逻辑类 WinnerCMS_ShoppingCart 的摘要说明。
    /// </summary>
    public class ShoppingCartManage
    {
        private static readonly ShoppingCartDal MyDAL = new ShoppingCartDal();

        #region 是否存在

        /// <summary>
        /// 是否存在
        /// </summary>
        public bool Exists(string CartGUID)
        {
            return MyDAL.Exists(CartGUID);
        }

        #endregion

        #region 加入购物车

        /// <summary>
        /// 加入购物车
        /// </summary>
        /// <param name="context"></param>
        public void JoinCart(HttpContext context)
        {
            int goodsId = int.Parse(context.Request.Form["GoodsID"]);

            HttpCookie myCookie = context.Request.Cookies["ShoppingCart"];
            string cartGuid = null;
            if (myCookie == null)
            {
                cartGuid = Guid.NewGuid().ToString();
                myCookie = new HttpCookie("ShoppingCart");
                myCookie.Values.Add("CartGUID", cartGuid);
                myCookie.Expires = DateTime.Now.AddDays(1);

                context.Response.Cookies.Add(myCookie);
            }
            else
            {
                cartGuid = myCookie.Values["CartGUID"];
            }

            ShoppingCart model = new ShoppingCart
            {
                GoodsId = goodsId,
                Quantity = 1,
                CartGUID = cartGuid,
                CheckInDate = DateTime.Now
            };

            MyDAL.Add(model);
        }

        #endregion

        #region 删除

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ID"></param>
        public Result Delete(int ID)
        {
            Result res = new Result();
            try
            {
                MyDAL.Delete(ID);
            }
            catch (Exception ex)
            {
                res.Errors = ex.Message;
            }
            return res;
        }

        #endregion

        #region 显示购物车状态

        /// <summary>
        /// 显示购物车状态
        /// </summary>
        /// <param name="CartGUID"></param>
        /// <returns>JSON格式</returns>
        public string ShowCartState(byte ModelId)
        {
            HttpCookie MyCookie = HttpContext.Current.Request.Cookies["ShoppingCart"];
            string CartGUID = null;
            if (MyCookie == null)
            {
                return string.Empty;
            }
            CartGUID = MyCookie.Values["CartGUID"];

            //var dr = MyDAL.ShowCartState(CartGUID);//dr 不会有null值

            //StringBuilder json = new StringBuilder();
            //json.AppendLine("{");
            //json.AppendLine("\"GoodsQuantity\": \"" + dr.GoodsQuantity + "\",");
            //json.AppendLine("\"PriceTotal\": \"" + ((decimal)dr["PriceTotal"]).ToString("C") + "\",");
            //json.AppendLine("\"CartUrl\": \"" + string.Format("/Member/ShoppingCart.aspx?ModelId={0}&CartGUID={1}", ModelId, CartGUID) + "\"");
            //json.AppendLine("}");

            DataRow dr = MyDAL.ShowCartState(CartGUID); //dr 不会有null值

            StringBuilder json = new StringBuilder();
            json.AppendLine("{");
            json.AppendLine("\"GoodsQuantity\": \"" + dr["GoodsQuantity"] + "\",");
            json.AppendLine("\"PriceTotal\": \"" + ((decimal) dr["PriceTotal"]).ToString("C") + "\",");
            json.AppendLine("\"CartUrl\": \"" +
                            string.Format("/Member/ShoppingCart.aspx?ModelId={0}&CartGUID={1}", ModelId, CartGUID) +
                            "\"");
            json.AppendLine("}");
            return json.ToString();
        }

        #endregion

        #region 显示购物车数据

        /// <summary>
        /// 显示购物车数据
        /// </summary>
        /// <returns></returns>
        public IList<ShoppingCart> LoadData()
        {
            HttpCookie MyCookie = HttpContext.Current.Request.Cookies["ShoppingCart"];
            string CartGUID = null;
            if (MyCookie == null)
            {
                return new List<ShoppingCart>();
            }
            CartGUID = MyCookie.Values["CartGUID"];

            return MyDAL.BindList(CartGUID, UserManage.Current.UserName, 1);
        }

        #endregion

        #region 清空

        /// <summary>
        /// 清空
        /// </summary>
        public void Clear(string CartGUID)
        {
            MyDAL.Clear(CartGUID);

            HttpCookie MyCookie = HttpContext.Current.Request.Cookies["ShoppingCart"];
            if (MyCookie != null)
            {
                MyCookie = new HttpCookie("ShoppingCart");

                MyCookie.Expires = DateTime.Now.AddMonths(-12);

                HttpContext.Current.Response.Cookies.Add(MyCookie);
            }
        }

        #endregion

        #region 修改购物车的数量

        /// <summary>
        /// 修改购物车的数量
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Result EditShoppingCartQuantity(HttpContext context)
        {
            Result res = new Result();
            int id;
            int Quantity;
            if (int.TryParse(context.Request.Form["ID"], out id) == false)
            {
                res.Errors = "ID 错误！";
                return res;
            }
            if (int.TryParse(context.Request.Form["Quantity"], out Quantity) == false)
            {
                res.Errors = "Quantity 错误！";
                return res;
            }
            try
            {
                MyDAL.EditShoppingCartQuantity(id, Quantity);

                return res;
            }
            catch (Exception ex)
            {
                res.Errors = ex.Message;
                return res;
            }
        }

        #endregion
    }
}